
axios.defaults.baseURL = 'https://autumnfish.cn';

            var app = new Vue({
                el: "#player",
                data: {
                    query: '',
                    musicList: [],
                    musicUrl: '',
                    isPlay: false,
                    lyric: '',
                    lyric_after: [],
                    hotComments: [],
                    coverUrl: '',
                    showVideo: false,
                    mvUrl: '',
                    start: '',
                    end: ''
                },

                methods: {
                    searchMusic() 
                    {
                        if (this.query == 0) {
                            return
                        }
                        axios.get('/search?keywords=' + this.query).then(response => {
                            this.musicList = response.data.result.songs;
                        })
                        this.query = ''
                        this.lyric = [];
                    },

                    playMusic(musicId) 
                    {
                        axios.get('/song/url?id=' + musicId).then(response => {
                            this.musicUrl = response.data.data[0].url

                            //console.log(this.musicUrl);
                        })

                        axios.get('/comment/hot?type=0&id=' + musicId).then(response => {
                            this.hotComments = response.data.hotComments

                            //console.log(this.hotComments);
                        })

                        axios.get('/song/detail?ids=' + musicId).then(response => {
                            this.coverUrl = response.data.songs[0].al.picUrl

                            //console.log(this.coverUrl);
                        })

                        axios.get('/lyric?id=' + musicId).then(response => {
                            this.lyric = response.data.lrc.lyric;
                        })
                    },

                    setLyric: function()
                    {
                        this.lyric_after = String(this.lyric);
                        const regNewLine = /\n/;
                        const lineArr = this.lyric_after.split(regNewLine)
                        
                        const regTime = /\[\d{2}:\d{2}.\d{2,3}\]/;
                        this.lyric_after = [];

                        lineArr.forEach(item => {
                            if (item === '') return
                               const obj = {}
                               const time = item.match(regTime)
                           
                               obj.lyric = item.split(']')[1].trim() === '' ? '' : item.split(']')[1].trim()
                               obj.time = time ? this.formatLyricTime(time[0].slice(1, time[0].length - 1)) : 0
                               obj.uid = Math.random().toString().slice(-6)

                               if (obj.lyric === '') {
                                   console.log('这一行没有歌词')
                                } else {
                                   this.lyric_after.push(obj)
                                }
                           })  
                    },

                    formatLyricTime: function(time) { // 格式化歌词的时间 转换成 sss:ms
                        const regMin = /.*:/
                        const regSec = /:.*\./
                        const regMs = /\./
                   
                        const min = parseInt(time.match(regMin)[0].slice(0, 2))
                        let sec = parseInt(time.match(regSec)[0].slice(1, 3))
                        const ms = time.slice(time.match(regMs).index + 1, time.match(regMs).index + 3)
                        if (min !== 0) {
                          sec += min * 60
                        }
                        return Number(sec + '.' + ms)
                    },

                    play() 
                    {
                        this.isPlay = true;
                        this.mvUrl = '';
                        this.lyric_after = [];
                        this.setLyric();
                        this.setTime();
                    },

                    pause() 
                    {
                        this.isPlay = false

                    },

                    playMv(vid) 
                    {
                        if (vid) 
                        {
                            this.showVideo = true;
                            axios.get('/mv/url?id=' + vid).then(response => {
                                this.$refs.audio.pause()
                                this.mvUrl = response.data.data.url
                            })
                        }
                    },

                    closeMv() 
                    {
                        this.showVideo = false
                        this.$refs.video.pause()
                    },

                    setTime()
                    {
                        for(var k = 0;k < this.lyric_after.length;k++)
                        {
                            var time = parseInt(this.lyric_after[k].time)
                            console.log(time);

                            var m;
                            var s;

                            if(time == 0)
                            {
                                m = 0;
                                s = 0;
                            }
                            else
                            {
                                for(var i = 1;(i * 60) < time;i++)
                                {
                                    m = i;
                                    s = parseInt( time - (60 * i) );
                                }
                            }

                            this.lyric_after[k].time = "[" + m + ":" + s  + "]";
                        }
                    }
                },
                

                watch: {
                    lyricCurrent()
                    {
                        this.lyric_after.forEach( (element,index) => {
                            if(this.lyricCurrent == element.time)
                            {
                                this.lyric
                            }
                        });
                    }
                }
            })
        